//
// Created by 21126 on 2023/5/19.
//

#ifndef CLION_TEST_U2_S5_H
#define CLION_TEST_U2_S5_H

#include "U2_i2c.h"

// PAGE 0：指令和状态
#define     RFU00                 0x00
#define     CommandReg            0x01
#define     ComIEnReg             0x02
#define     DivlEnReg             0x03
#define     ComIrqReg             0x04
#define     DivIrqReg             0x05
#define     ErrorReg              0x06
#define     Status1Reg            0x07
#define     Status2Reg            0x08
#define     FIFODataReg           0x09
#define     FIFOLevelReg          0x0A
#define     WaterLevelReg         0x0B
#define     ControlReg            0x0C
#define     BitFramingReg         0x0D
#define     CollReg               0x0E
#define     RFU0F                 0x0F
// PAGE 1：指令
#define     RFU10                 0x10
#define     ModeReg               0x11
#define     TxModeReg             0x12
#define     RxModeReg             0x13
#define     TxControlReg          0x14
#define     TxAutoReg             0x15
#define     TxSelReg              0x16
#define     RxSelReg              0x17
#define     RxThresholdReg        0x18
#define     DemodReg              0x19
#define     RFU1A                 0x1A
#define     RFU1B                 0x1B
#define     MifareReg             0x1C
#define     RFU1D                 0x1D
#define     RFU1E                 0x1E
#define     SerialSpeedReg        0x1F
// PAGE 2：配置
#define     RFU20                 0x20
#define     CRCResultRegM         0x21
#define     CRCResultRegL         0x22
#define     RFU23                 0x23
#define     ModWidthReg           0x24
#define     RFU25                 0x25
#define     RFCfgReg              0x26
#define     GsNReg                0x27
#define     CWGsCfgReg            0x28
#define     ModGsCfgReg           0x29
#define     TModeReg              0x2A
#define     TPrescalerReg         0x2B
#define     TReloadRegH           0x2C
#define     TReloadRegL           0x2D
#define     TCounterValueRegH     0x2E
#define     TCounterValueRegL     0x2F
// PAGE 3   测试寄存器
#define     RFU30                 0x30
#define     TestSel1Reg           0x31
#define     TestSel2Reg           0x32
#define     TestPinEnReg          0x33
#define     TestPinValueReg       0x34
#define     TestBusReg            0x35
#define     AutoTestReg           0x36
#define     VersionReg            0x37
#define     AnalogTestReg         0x38
#define     TestDAC1Reg           0x39
#define     TestDAC2Reg           0x3A
#define     TestADCReg            0x3B
#define     RFU3C                 0x3C
#define     RFU3D                 0x3D
#define     RFU3E                 0x3E
#define     RFU3F		              0x3F

#define RESET_COMMAND   0x0F

#define MI_OK                      0
#define MI_NOTAGERR                1
#define MI_ERR                     2

#define MAXRLEN                    18


#define S5_NUM                     4

typedef struct{
    i2c_addr_def nfc_addr[4];
} s5_addr_def;

i2c_addr_def u2_s5_init(int fd);
void u2_ms253_init(int fd, __u8 slave_address);
void u2_clear_bit_mask(int fd, __u8 slave_address, __u8 reg_address, __u8 mask);
void u2_set_bit_mask(int fd, __u8 slave_address, __u8 reg_address, __u8 mask);
__u8 u2_pcd_request(int fd, __u8 slave_address, __u8 req_code, __u8 *tag_type);
__u8 u2_pcd_anti_collision(int fd, __u8 slave_address, __u8 *snr);
__u8 u2_pcd_select(int fd, __u8 slave_address, __u8 *snr);
__u8 u2_pcd_auth_state(int fd, __u8 slave_address, __u8 mode, __u8 len, __u8 *key, __u8 *snr);
__u8 u2_pcd_read(int fd, __u8 slave_address, __u8 len, __u8 *data);
__u8 u2_pcd_write(int fd, __u8 slave_address, __u8 len, __u8 *data);
__u8 u2_pcd_halt(int fd, __u8 address);
__u8 u2_pcd_commands_523(int fd, __u8 i2c_addr, __u8 Command, __u8 *pInData, __u8 InLenByte, __u8 *pOutData, __u16  *pOutLenBit);
void u2_calculate_CRC(int fd, __u8 address, __u8 *in_data, __u8 len, __u8 *out_data);
__u8 u2_s5_detect(int fd, __u8 slave_address, __u8 *card_num);
__u8 u2_s5_verify(int fd, __u8 slave_address, __u8 auth_mode, __u8 len, __u8 *Key, __u8 *snr);
__u8 u2_s5_write_data(int fd, __u8 slave_address, __u8 len, __u8 *data);
__u8 u2_s5_read_data(int fd, __u8 slave_address, __u8 len, __u8 *data);
void u2_s5_sleep(int fd, __u8 slave_address);
#endif //CLION_TEST_U2_S5_H
